<?php include 'header.php'; ?>
            <h2>修改文章</h2>
            <?php
            include 'db_connect.php';

            if (!isset($_GET['id'])) {
                header("Location: index.php");
                exit();
            }

            $post_id = $_GET['id'];
            $sql = "SELECT * FROM posts WHERE id = $post_id";
            $result = $conn->query($sql);

            if ($result->num_rows == 0) {
                echo '<p>帖子不存在！</p>';
                include 'footer.php';
                exit();
            }

            $post = $result->fetch_assoc();

            // 检查当前用户是否为作者
            if ($_SESSION['username'] != $post["author"]) {
                echo '<p>您没有权限修改这篇文章！</p>';
                include 'footer.php';
                exit();
            }
            ?>

            <form method="post" action="update_post.php">
                <input type="hidden" name="id" value="<?php echo $post['id']; ?>">
                <label for="title">标题：</label>
                <input type="text" id="title" name="title" value="<?php echo $post['title']; ?>" required>
                
                <label for="content">内容：</label>
                <textarea id="content" name="content" rows="10" required><?php echo $post['content']; ?></textarea>
                
                <button type="submit" class="btn">更新文章</button>
                <a href="post.php?id=<?php echo $post['id']; ?>" class="btn">取消</a>
            </form>
<?php include 'footer.php'; ?>
